﻿ using System;
using System.Xml;
using System.Configuration;

namespace Fawcett.Plugins.FSUIPCX
{

    public class OffsetSectionHandler : IConfigurationSectionHandler
    {
        public event PluginEventHandler PluginSendEvent;

        public OffsetSectionHandler()
        {
        }
        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {

            OffsetCollection offsets = new OffsetCollection();

            foreach (XmlNode node in section.ChildNodes)
            {
                try
                {
                    offsets.Add(new Offset()
                    {
                        Name = node.Attributes["Name"].Value,
                        Address = node.Attributes["Address"].Value,
                        Type = node.Attributes["Type"].Value,
                        Calculate = node.Attributes["Calculate"].Value
                    });

                }
                catch (Exception e)
                {
                    string str = String.Format(Fawcett.Plugins.Properties.Resources.ERROR_3, section.Name);
                    Fawcett.Plugins.MessageBox.Show(e, str);
                }
            }

            return offsets;
        }

        public void RaisePluginEvent(Object source, EPlugin p)
        {
            PluginSendEvent(source, p);
        }
        #endregion
    }

}